import re
import scrapy
import urllib.parse
from drug_data.items import ConedosItem


class ConedosSpider(scrapy.Spider):
    name = 'conedos'
    allowed_domains = ['360kad.com']
    start_urls = ['https://www.360kad.com/Category_45/Index.aspx']

    def parse(self, response):
        lis = response.css('ul.YLinkList li')  # 医科分类
        for li in lis:
            name = li.css('a::text').extract_first()  # 医科名
            href_url = li.css('a::attr(href)').extract_first()  # 详情链接
            href_url = urllib.parse.urljoin(response.url, href_url)  # 拼接完整链接
            # print(name,href_url)

            yield scrapy.Request(
                url=href_url, callback=self.get_detail_url  # get_next_url
            )

    # # 获取翻页链接
    # def get_next_url(self,response):
    #     # 总页数
    #     next_num = response.xpath('//span[@class="first"]/text()').extract_first()
    #     next_num = re.sub(r'[\n\t\s共个商品]','',next_num)
    #     # print(round(int(next_num)//20,1))
    #     for pn in range(1,int(next_num)//20 + 2,1):
    #         next_url = response.url + f'?page={pn}'
    #         # print(next_url)
    #
    #         yield scrapy.Request(
    #             url=next_url,callback=self.get_detail_url
    #         )

    # 获取详情页链接
    def get_detail_url(self, response):
        lis = response.xpath('//ul[@id="YproductList"]/li')  # 定义药品节点
        for li in lis:
            title = li.xpath('.//p[@class="Yadv"]/text()').extract_first()  # 标题
            discount_price = li.xpath('.//p[@class="Ypribox"]/span[@class="RMB"]/text()').get().strip('￥')  # 折扣价
            orginal_price = li.xpath('.//p[@class="Ypribox"]/i[@class="RMB"]/text()').get().strip('￥')  # 原价
            detail_url = li.xpath('.//p[@class="Ypic"]/a/@href').extract_first()  # 药品详情页链接
            detail_url = urllib.parse.urljoin(response.url, detail_url)  # 拼接完整链接
            # print(title,discount_price,orginal_price,detail_url)

            yield scrapy.Request(
                url=detail_url, callback=self.get_detail_data, meta={'dprice': discount_price, 'oprice': orginal_price}
            )

        # 翻页
        next_url = response.xpath('//span[@class="end"]/a[1]/@href').extract_first()
        next_url = urllib.parse.urljoin(response.url, next_url)
        # print(next_url == response.url)
        if next_url != response.url:  # 判断下一页链接和请求链接是否相同
            yield scrapy.Request(
                url=next_url, callback=self.get_detail_url
            )
        else:
            return

    # 获取详情页数据
    def get_detail_data(self, response):
        drug_id = response.url.split('/')[-1].split('.')[0]  # 药品id
        sort = response.xpath('//div[@class="wrap-dtl-nav"]/a[3]/@title').extract_first()  # 类别
        symptom = response.xpath('//div[@class="wrap-dtl-nav"]/a[4]/@title').extract_first()  # 症状名
        ul = response.xpath('//div[@class="infor-list"]/ul')
        drug_name = ul.xpath('./li[1]/@title').extract_first()  # 药品名
        discount_price = response.meta['dprice']  # 折扣价
        orginal_price = response.meta['oprice']  # 原价

        com_name = response.xpath(
            '//div[@class="dtl-inf-top"]/div[1]/div[@class="dtl-inf-r"]/text()').extract_first()  # 通用名称
        approve_num = response.xpath('//div[@class="dtl-inf-top"]/div[2]/div[@class="dtl-inf-r"]/text()').get()  # 批准文号
        approve_num = re.sub(r'[\t\n\s]', '', approve_num)

        manufacturer = response.xpath(
            '//div[@class="dtl-inf-top"]/div[3]/div[@class="dtl-inf-r"]/text()').extract_first()  # 生产厂家

        # 商品介绍
        specification = ul.xpath('./li[2]/@title').extract_first()  # 产品规格
        valid_period = ul.xpath('./li[6]/@title').extract_first()  # 有效期
        shape = ul.xpath('./li[7]/@title').extract_first()  # 形状
        # 主要作用
        effect = ul.xpath('./li[9]/@title').extract_first()
        if effect:
            effect = re.sub(r'[\t\n\s]', '', effect)
        else:
            effect = None

        # 用法用量
        dosage = ul.xpath('./li[10]/@title').extract_first()
        if dosage:
            dosage = re.sub(r'[\t\n\s]', '', dosage)
        else:
            dosage = None

        # 不良反应
        bedreply = ul.xpath('./li[11]/@title').extract_first()
        if bedreply:
            bedreply = re.sub(r'[\t\n\s]', '', bedreply)
        else:
            bedreply = None

        item = ConedosItem(
            _id=drug_id, sort=sort, symptom=symptom, drug_name=drug_name, discount_price=discount_price,
            orginal_price=orginal_price, com_name=com_name, approve_num=approve_num, manufacturer=manufacturer,
            specification=specification, valid_period=valid_period, shape=shape, effect=effect, dosage=dosage,
            bedreply=bedreply
        )
        # print(item)
        yield item